<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="消息全局ID" prop="Mid">
        <el-input
          v-model="queryParams.Mid"
          placeholder="请输入消息全局ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送人" prop="From">
        <el-input
          v-model="queryParams.From"
          placeholder="请输入发送人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会话目标" prop="Target">
        <el-input
          v-model="queryParams.Target"
          placeholder="请输入会话目标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务线" prop="Line">
        <el-input
          v-model="queryParams.Line"
          placeholder="请输入业务线"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务器接收时间" prop="Dt">
        <el-date-picker clearable
          v-model="queryParams.Dt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择服务器接收时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="${comment}" prop="To">
        <el-input
          v-model="queryParams.To"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:messages:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:messages:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:messages:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:messages:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messagesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="消息全局ID" align="center" prop="Mid" />
      <el-table-column label="发送人" align="center" prop="From" />
      <el-table-column label="消息类型" align="center" prop="Type" />
      <el-table-column label="会话目标" align="center" prop="Target" />
      <el-table-column label="业务线" align="center" prop="Line" />
      <el-table-column label="protobuf 二进制消息体" align="center" prop="Data" />
      <el-table-column label="全文搜索关键字" align="center" prop="SearchableKey" />
      <el-table-column label="服务器接收时间" align="center" prop="Dt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.Dt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="${comment}" align="center" prop="To" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:messages:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:messages:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="消息全局ID" prop="Mid">
          <el-input v-model="form.Mid" placeholder="请输入消息全局ID" />
        </el-form-item>
        <el-form-item label="发送人" prop="From">
          <el-input v-model="form.From" placeholder="请输入发送人" />
        </el-form-item>
        <el-form-item label="会话目标" prop="Target">
          <el-input v-model="form.Target" placeholder="请输入会话目标" />
        </el-form-item>
        <el-form-item label="业务线" prop="Line">
          <el-input v-model="form.Line" placeholder="请输入业务线" />
        </el-form-item>
        <el-form-item label="全文搜索关键字" prop="SearchableKey">
          <el-input v-model="form.SearchableKey" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="服务器接收时间" prop="Dt">
          <el-date-picker clearable
            v-model="form.Dt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择服务器接收时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="To">
          <el-input v-model="form.To" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMessages, getMessages, delMessages, addMessages, updateMessages } from "@/api/system/messages"

export default {
  name: "Messages",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      messagesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        Mid: null,
        From: null,
        Type: null,
        Target: null,
        Line: null,
        Data: null,
        SearchableKey: null,
        Dt: null,
        To: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        Mid: [
          { required: true, message: "消息全局ID不能为空", trigger: "blur" }
        ],
        From: [
          { required: true, message: "发送人不能为空", trigger: "blur" }
        ],
        Type: [
          { required: true, message: "消息类型不能为空", trigger: "change" }
        ],
        Target: [
          { required: true, message: "会话目标不能为空", trigger: "blur" }
        ],
        Line: [
          { required: true, message: "业务线不能为空", trigger: "blur" }
        ],
        Data: [
          { required: true, message: "protobuf 二进制消息体不能为空", trigger: "blur" }
        ],
        Dt: [
          { required: true, message: "服务器接收时间不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      listMessages(this.queryParams).then(response => {
        this.messagesList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        Mid: null,
        From: null,
        Type: null,
        Target: null,
        Line: null,
        Data: null,
        SearchableKey: null,
        Dt: null,
        To: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加【请填写功能名称】"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMessages(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改【请填写功能名称】"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMessages(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addMessages(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delMessages(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/messages/export', {
        ...this.queryParams
      }, `messages_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
